package com.gitee.wsl.db.sql

import kotlin.jvm.JvmStatic


class Order private constructor(private val expression: Any, val orderType: OrderType = OrderType.ASC) {
    private val secondaryExpressions = ArrayList<Order>()

    fun addSecondaryExpression(secondary: Order): Order {
        secondaryExpressions.add(secondary)
        return this
    }

    override fun toString() =
            "$expression $orderType${secondaryExpressions.takeIf { it.isNotEmpty() }?.joinToString(", ", ", ") ?: ""}"

    fun reverse() = Order(expression, if (orderType === OrderType.ASC) OrderType.DESC else OrderType.ASC)

    companion object {
        @JvmStatic
        fun asc(expression: Any) = Order(expression)

        @JvmStatic
        fun desc(expression: Any) = Order(expression, OrderType.DESC)
    }
}